import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-06-28
 * Time: 10:33
 */
public class test4 {
    public int subarraySum(int[] nums, int k) {
        // key是sum，value是sum出现的次数
        Map<Integer,Integer> map = new HashMap<Integer,Integer>();
        int sum = 0;
        int n = nums.length;
        // 边界值
        map.put(0,1);
        int count = 0;
        for(int i=0;i<n;i++){
            sum += nums[i];
            if(map.containsKey(sum-k)){
                count += map.get(sum-k);
            }
            // 更新sum在哈希表中的值
            map.put(sum,map.getOrDefault(sum,0)+1);
        }
        return count;
    }
}
